package pl.edu.mimuw.irs.core.model.design.net;

import java.util.LinkedHashSet;
import java.util.Set;

import pl.edu.mimuw.irs.core.global.IDManager;
import pl.edu.mimuw.irs.core.model.petrinet.Net;
import pl.edu.mimuw.irs.core.util.Converter;
import pl.edu.mimuw.irs.core.util.Position;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-07
 */
public class IRSActivity extends IRSTransition {
	
	private static final long serialVersionUID = 6246018324000613033L;

	// *** Attributes
	
	private IRSRequirement requirement;
	// Node supports
	private final static Set<Fragmentation> supportedFragmentations;
	static {
		supportedFragmentations = new LinkedHashSet<Fragmentation>();
		supportedFragmentations.add(Fragmentation.SEQUENCE);
		supportedFragmentations.add(Fragmentation.ALTERNATIVE);
		supportedFragmentations.add(Fragmentation.NONE);
	}
	public IRSRequirement getRequirement() {
		return Converter.asIRSRequirement(getParent().getChildren().get(1)); // XXX dirty
	}
	@Override
	public Set<Fragmentation> getSupportedFragmentations() {
		return supportedFragmentations;
	}
	public int getTimeUnits() {
		return getRequirement().getTimeUnits();
	}
	@Override
	public IRSTransitionCopier getCopier() {
		return new IRSActivityCopier(this);
	}
	
	// *** Constructors
	
	public IRSActivity(IDManager idManager, String newId, NodeType nodeType, Position position) {
		super(idManager, newId, nodeType, position);
	}
	
	public IRSActivity(IDManager idManager, String id, Position position) {
		super(idManager, id, position);
	}

	public IRSActivity(IDManager idManager, Position position) {
		super(idManager, position);
	}
	
	public IRSActivity(IDManager idManager) {
		super(idManager);
	}
	
	// *** Methods
	
	@Override
	public void setNet(Net net) {
		super.setNet(net);

		if (net != null) {
			IRSWorkflow workflow = Converter.asIRSWorkflow(getNet());
			workflow.getActivities().add(this);
			workflow.getRequirements().add(requirement);
		}
	}
	
	@Override
	public void detach() {
		if (getNet() != null) {
			IRSWorkflow workflow = Converter.asIRSWorkflow(getNet());
			workflow.getActivities().remove(this);
			workflow.getRequirements().remove(getRequirement());
		}
		super.detach();
	}
}
